package com.yeung.spel;

import com.yeung.primary.Address;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpEl {
    @Test
    public void 测试spel表达式() {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans-spel.xml");
        Address address = (Address) context.getBean("address");
        System.out.println(address);
        Car car = (Car) context.getBean("car");
        System.out.println(car);
        Person p = (Person) context.getBean("person");
        System.out.println(p);
    }

    @Test
    public void 测试bean的生命周期() {

        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans-cycle.xml");
        Car car = (Car) context.getBean("car");
        System.out.println(car);

    }

    @Test
    public void 测试工厂类静态创建对象的配置() {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans-factory.xml");
        Car car = (Car) context.getBean("car");
        System.out.println(car);
    }

    @Test
    public void 测试工厂类创建实例对象的配置() {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans-factory.xml");
        Car car = (Car) context.getBean("car2");
        System.out.println(car);
    }

    /**
     * 实现spring的beanfactory接口, 由spring来负责创建bean对象(方便再其他bena要使用该类的实例时)
     * 配置的class指向的时beanfactory的类型,
     * 但是返回的是我们beanfactory类里指定的car类型(getObject方法)
     * 由此创建了一个car实例
     */
    @Test
    public void 测试FactoryBean创建实例对象的配置() {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans-factory.xml");
        Car car = (Car) context.getBean("car3");
        System.out.println(car);
    }

}

